function cellOut = convert2tex(cellIn,reserveWords)
% cellIn: cell containing strings to be converted into latex expressions
% reserveWords: optional list of words that have precedence over greek
% letters and will not get matched...i.e. we do not want to match "eta" in
% "sharetax", for example.

%% Inevitable Bugs!!!
% Code assumes upper case greek letters are written in all caps, i.e. write
% DELTA if you mean \Delta and
%%

% Append "ambiguous" words to reserve words.  We do not want to replace
% theta with \th\eta, so we need to handle some cases with care
if nargin < 2
    reserveWords = {'theta', 'beta', 'zeta', 'THETA', 'BETA', 'ZETA', 'varrho', 'varepsilon',...
        'varpi', 'varsigma', 'varphi', 'varkappa'};
else
    reserveWords = [reserveWords, 'theta', 'beta', 'zeta', 'THETA', 'BETA', 'ZETA', 'varrho',...
        'varepsilon', 'varpi', 'varsigma', 'varphi', 'varkappa'];
end

nStrings = length(cellIn);
nReserveWords = length(reserveWords);
cellOut = cell(nStrings,1);

% Note that theta, beta, and zeta are missing.  This is because we will have to handle it
% separately so as not to confuse "eta" as a substring
greek = {'alpha', 'gamma', 'delta', 'epsilon', 'eta', 'iota', 'kappa', ...
    'lambda', 'mu', 'nu', 'xi', 'omicron', 'pi', 'rho', 'sigma', 'tau', 'upsilon', 'phi', 'chi', 'psi',...
    'omega'};

% Insert @@@ before and after each reserve word.  This helps to distinguish
% them later in the code
bufferedReserveWords = cell(nReserveWords,1);
for k = 1:nReserveWords
    bufferedReserveWords{k} = ['@@@' reserveWords{k} '@@@'];
end

% Format greek to \\greek
formattedGreek = cell(length(greek),1);
formattedUpperGreek = cell(length(greek),1);
for k = 1:length(greek)
    formattedGreek{k} = ['\\\\' greek{k} ' '];
    temp = greek{k};
    formattedUpperGreek{k} = ['\\\\' upper(temp(1)) temp(2:end) ' '];
end

for k = 1:nStrings
    
    % Replace reserve words with buffered reserve words
    temp = regexprep(cellIn{k}, reserveWords, bufferedReserveWords);
    
    % Split input string on @@@.  This helps to identify reserve words
    temp = regexp(temp,'@@@','split');
    
    nChunks = length(temp);
    
    for m = 1:nChunks
        
        % Check to see whether chunk is reserve word or not
        isNotReserveWord = ~any(strcmp(temp{m}, reserveWords));
        
        if isNotReserveWord
            % Replace greek with \\greek
            temp{m} = regexprep(temp{m}, greek, formattedGreek);
            temp{m} = regexprep(temp{m}, upper(greek), formattedUpperGreek);
        else
            % Now handle the theta, beta, zeta cases, if they exist
            temp{m} = regexprep(temp{m}, {'theta', 'beta', 'zeta'}, {'\\\\theta ', '\\\\beta ', '\\\\zeta '});
            temp{m} = regexprep(temp{m}, {'THETA', 'BETA', 'ZETA'}, {'\\\\Theta ', '\\\\Beta ', '\\\\Zeta '});
            
            % Now handle varrho, varepsilon, varpi, varsigma, varphi,
            % varkappa, vartheta
            temp{m} = regexprep(temp{m}, {'varrho','varepsilon', 'varpi', 'varsigma', 'varphi', 'varkappa'},...
                {'\\\\varrho ','\\\\varepsilon ', '\\\\varpi ', '\\\\varsigma ', '\\\\varphi ', ...
                '\\\\varkappa '});
        end
        
    end
    
    % Concatenate chunks of strings
    temp = cell2mat(temp);
    
    temp = parseAsLatexMath(temp);
    
%     % Replace _ with _{}
%     hasUnderscore = (regexp(temp,'_') > 0);
%     if hasUnderscore
%         temp = regexprep(temp,'_','_{');
%         temp = [temp  '}'];
%     end
    
    % Add $ if the expression needs to be set in math-mode
    if ~(length(regexp(temp,'\w')) == length(temp))
        temp = ['$' temp '$'];
    end
    %     hasMath = (length(strfind(temp,'\')) > 0 ) || (length(strfind(temp,'_')) > 0);
    %
    %     if hasMath
    %         temp = ['$' temp '$'];
    %     end
    %
    cellOut{k} = temp;
    
end
end
